package com.resource.authoritymanage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.resource.authoritymanage.config.ApplyStatusEnum;
import com.resource.authoritymanage.entity.TIdentifyApplyEntity;
import com.resource.authoritymanage.entity.TIdentifyAuditEntity;
import com.resource.authoritymanage.feign.SysUserRoleControlRemote;
import com.resource.authoritymanage.mapper.TIdentifyApplyMapper;
import com.resource.authoritymanage.service.TIdentifyApplyService;
import com.resource.authoritymanage.service.TIdentifyAuditService;
import com.resource.common.utils.AppResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.resource.common.utils.PageUtils;
import com.resource.common.utils.Query;


@Service
public class TIdentifyApplyServiceImpl extends ServiceImpl<TIdentifyApplyMapper, TIdentifyApplyEntity> implements TIdentifyApplyService {

    @Autowired
    private TIdentifyAuditService tIdentfiyAuditService;

    @Autowired
    private SysUserRoleControlRemote sysUserRoleControlRemote;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Object applyNo = params.get("applyNo");
        int size = params.size();
        IPage<TIdentifyApplyEntity> page=new Page<>();
        if(size==4){
            String keys="";
            String param="";
            Set<String> strings = params.keySet();
            for (String key : strings) {
                keys=key;
                param=params.get(keys).toString();
            }
            page = this.page(
                    new Query<TIdentifyApplyEntity>().getPage(params),
                    new QueryWrapper<TIdentifyApplyEntity>().eq(keys,param)
            );
            return new PageUtils(page);
        }
        //获取当前用户下的申请信息
        page = this.page(
                new Query<TIdentifyApplyEntity>().getPage(params),
                new QueryWrapper<TIdentifyApplyEntity>()
        );

        page.getRecords().stream().forEach(
                item->{
                    item.setApplyStatus(ApplyStatusEnum.parseName(item.getApplyStatus()));
                }
        );
        return new PageUtils(page);
    }

    public AppResponse submit(TIdentifyApplyEntity tIdentifyApply){
        if(StringUtils.isEmpty(tIdentifyApply.getApplyNo())){
            this.save(tIdentifyApply);
        }else{
            this.saveOrUpdate(tIdentifyApply);
        }
        //提交申请生成一条审核记录
        TIdentifyAuditEntity tIdentifyAuditEntity = new TIdentifyAuditEntity();
        tIdentifyAuditEntity.setApplyNo(tIdentifyApply.getApplyNo());
        tIdentifyAuditEntity.setApplyName(tIdentifyApply.getApplyName());
        tIdentifyAuditEntity.setApplyPrivilege(tIdentifyApply.getApplyPrivilege());
        tIdentifyAuditEntity.setApplyReason(tIdentifyApply.getApplyReason());
        tIdentifyAuditEntity.setApplyType(tIdentifyApply.getApplyType());
        tIdentifyAuditEntity.setAuditStatus("未审批");
        tIdentfiyAuditService.save(tIdentifyAuditEntity);
        return AppResponse.success();
    }

    @Override
    public void updateByAuditResult(String auditResult,String applyNo) {
        TIdentifyAuditEntity updateIdentify = tIdentfiyAuditService.getOne(new QueryWrapper<TIdentifyAuditEntity>().eq("apply_no", applyNo));
        TIdentifyApplyEntity applyIdentify = this.getOne(new QueryWrapper<TIdentifyApplyEntity>().eq("apply_no", applyNo));
        String applyPrivilege = applyIdentify.getApplyPrivilege();
        //审批通过
        if(!Objects.isNull(applyIdentify)) {
            if (auditResult.equals("true")) {
                applyIdentify.setApplyStatus("2");
                //远程调用修改权限表
                Map<String, String> map = new HashMap<>();
                map.put("identifyApplyName",applyIdentify.getApplyName());
                map.put("identifyPrivilege",applyPrivilege);
                sysUserRoleControlRemote.accepPrivilege(map);
            } else {
                applyIdentify.setApplyStatus("3");
            }
            applyIdentify.setApplyRemarks(updateIdentify.getAuditRemark());
            this.getBaseMapper().updateById(applyIdentify);

        }
    }

}